package cn.plushine.controller;

import cn.plushine.pojo.*;
import cn.plushine.service.BaseCheckService;
import cn.plushine.service.BaseTransferService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 盘点订单
 */
@Api(tags = "盘点订单")
@Controller
@RestController
@RequestMapping("/admin/product/check")
public class BaseCheckController {

    @Autowired
    BaseCheckService baseCheckService;

    // 获取盘点订单
    @ApiOperation(value = "获取盘点订单")
    @GetMapping("/get")
    public HashMap<String, Object> get() {
        try {
            // 获取盘点订单
            List<Check> checksDB = baseCheckService.get();
            // 计算货物总数、总盘点数、订单状态等
            for (Check check : checksDB) {
                // 货物总数
                check.setSum(check.getChecks().size());
                int flag = 0;
                for (CheckItem checkItem : check.getChecks()) {
                    flag += checkItem.getFlag();
                }
                check.setFlag(flag == 0 ? 0 : (flag == check.getChecks().size() ? 2 : 1));
            }
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取盘点订单成功！", checksDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取盘点订单失败！", e.getMessage());
        }
    }

//    // 获取仓库信息（库-区-位）
//    @ApiOperation(value = "获取仓库信息（库-区-位）")
//    @GetMapping("/warehouse")
//    public HashMap<String, Object> warehouse() {
//        try {
//            // 获取仓库信息（库-区-位）
//            List<Cascader> warehouseDB = baseTransferService.warehouse();
//            // 成功响应
//            return ResMapUtils.encapsulation(200, "获取仓库信息（库-区-位）成功！", warehouseDB);
//        } catch (Exception e) {
//            // 失败响应
//            return ResMapUtils.encapsulation(201, "获取仓库信息（库-区-位）失败！", e.getMessage());
//        }
//    }

    // 获取当前盘点位置下的货物信息
    @ApiOperation(value = "获取当前盘点位置下的货物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouse", value = "仓库id", required = true, paramType = "path", dataType = "int", example = "0"),
            @ApiImplicitParam(name = "area", value = "分区id", required = true, paramType = "path", dataType = "int", example = "0"),
            @ApiImplicitParam(name = "position", value = "库位id", required = true, paramType = "path", dataType = "int", example = "0")
    })
    @GetMapping("/getChecks/{warehouse}/{area}/{position}")
    public HashMap<String, Object> getChecks(@PathVariable int warehouse, @PathVariable int position, @PathVariable int area) {
        try {
            // 获取当前盘点位置下的货物信息
            List<CheckItem> checksDB = baseCheckService.getChecks(warehouse, area, position);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取当前盘点位置下的货物信息成功！", checksDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取当前盘点位置下的货物信息失败！", e.getMessage());
        }
    }

    // 生成盘点订单id
    @ApiOperation(value = "生成盘点订单id")
    @GetMapping("/getId")
    public HashMap<String, Object> getId() {
        try {
            // 生成盘点订单id
            String id = "PD" + System.currentTimeMillis();
            // 成功响应
            return ResMapUtils.encapsulation(200, "生成盘点订单id成功！", id);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "生成盘点订单id失败！", e.getMessage());
        }
    }


//    // 查询货物单位（库存、品牌）
//    @ApiOperation(value = "查询货物单位（库存、品牌）")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "货物id", required = true, paramType = "path", dataType = "int", example = "1"),
//            @ApiImplicitParam(name = "o3", value = "库位id", required = true, paramType = "path", dataType = "int", example = "1")
//    })
//    @GetMapping("/getUnit/{id}/{o3}")
//    public HashMap<String, Object> getUnit(@PathVariable int id, @PathVariable int o3) {
//        try {
//            // 查询货物单位（库存、品牌）
//            TransferItem unitDB = baseTransferService.getUnit(id, o3);
//            // 成功响应
//            return ResMapUtils.encapsulation(200, "查询货物单位（库存、品牌）成功！", unitDB);
//        } catch (Exception e) {
//            // 失败响应
//            return ResMapUtils.encapsulation(201, "查询货物单位（库存、品牌）失败！", e.getMessage());
//        }
//    }

    // 添加盘点订单
    @ApiOperation(value = "添加盘点订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "check", value = "内容", required = true, paramType = "body", dataType = "Check")
    })
    @PostMapping("/add")
    public HashMap<String, Object> add(@RequestBody Check check) {
        try {
            System.out.println(check);
            // 添加盘点订单
            baseCheckService.add(check);
            // 给货物绑定盘点订单
            baseCheckService.addItem(check);
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加盘点订单成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "添加盘点订单失败！", e.getMessage());
        }
    }


    // 批量盘点
    @ApiOperation(value = "批量盘点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "货物盘后状态", paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "check", value = "盘点订单信息", paramType = "body", dataType = "Check")
    })
    @PutMapping("/modify/{status}")
    public HashMap<String, Object> modify(@RequestBody Check check, @PathVariable int status) {
        try {
            System.out.println(status + "***" + check);
            // 批量盘点
            baseCheckService.modify(check, status);
            // 成功响应
            return ResMapUtils.encapsulation(200, "批量盘点成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "批量盘点失败！", e.getMessage());
        }
    }

    // 取消盘点订单
    @ApiOperation(value = "取消盘点订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "盘点订单id", required = true, paramType = "path", dataType = "String")
    })
    @DeleteMapping("/cancel/{id}")
    public HashMap<String, Object> cancel(@PathVariable String id) {
        try {
            // 删除盘点订单
            baseCheckService.delete(id);
            // 恢复货物信息
            baseCheckService.cancel(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "取消盘点订单成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "取消盘点订单失败！", e.getMessage());
        }
    }
}
